package com.jeegit.ecms.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.jeegit.common.core.domain.Ztree;
import com.jeegit.common.utils.DateUtils;
import com.jeegit.common.utils.IdUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeegit.ecms.mapper.CmsArticleCategoryMapper;
import com.jeegit.ecms.domain.CmsArticleCategory;
import com.jeegit.ecms.service.ICmsArticleCategoryService;
import com.jeegit.ecms.utils.ECMSUtils;
import com.jeegit.common.core.text.Convert;

/**
 * 文章分类Service业务层处理
 * 
 * @author jeegit
 * @date 2022-06-25
 */
@Service
public class CmsArticleCategoryServiceImpl implements ICmsArticleCategoryService 
{
    @Autowired
    private CmsArticleCategoryMapper cmsArticleCategoryMapper;

    /**
     * 查询文章分类
     * 
     * @param id 文章分类主键
     * @return 文章分类
     */
    @Override
    public CmsArticleCategory selectCmsArticleCategoryById(Long id)
    {
        return cmsArticleCategoryMapper.selectCmsArticleCategoryById(id);
    }

    /**
     * 查询文章分类列表
     * 
     * @param cmsArticleCategory 文章分类
     * @return 文章分类
     */
    @Override
    public List<CmsArticleCategory> selectCmsArticleCategoryList(CmsArticleCategory cmsArticleCategory)
    {
        return cmsArticleCategoryMapper.selectCmsArticleCategoryList(cmsArticleCategory);
    }

    /**
     * 新增文章分类
     * 
     * @param cmsArticleCategory 文章分类
     * @return 结果
     */
    @Override
    public int insertCmsArticleCategory(CmsArticleCategory cmsArticleCategory)
    {
    	cmsArticleCategory.setId(IdUtils.getInstance(3l).nextId());
    	cmsArticleCategory.preInsert(cmsArticleCategory);
    	return cmsArticleCategoryMapper.insertCmsArticleCategory(cmsArticleCategory);
    }

    /**
     * 修改文章分类
     * 
     * @param cmsArticleCategory 文章分类
     * @return 结果
     */
    @Override
    public int updateCmsArticleCategory(CmsArticleCategory cmsArticleCategory)
    {
    	cmsArticleCategory.preUpdate(cmsArticleCategory);
        return cmsArticleCategoryMapper.updateCmsArticleCategory(cmsArticleCategory);
    }

    /**
     * 批量删除文章分类
     * 
     * @param ids 需要删除的文章分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleCategoryByIds(String ids)
    {
        return cmsArticleCategoryMapper.deleteCmsArticleCategoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文章分类信息
     * 
     * @param id 文章分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleCategoryById(Long id)
    {
        return cmsArticleCategoryMapper.deleteCmsArticleCategoryById(id);
    }

    /**
     * 查询文章分类树列表
     * 
     * @return 所有文章分类信息
     */
    @Override
    public List<Ztree> selectCmsArticleCategoryTree()
    {
        List<CmsArticleCategory> cmsArticleCategoryList = cmsArticleCategoryMapper.selectCmsArticleCategoryList(new CmsArticleCategory());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        ztrees=ECMSUtils.addTreeRoot(ztrees);//追加根目录
        for (CmsArticleCategory cmsArticleCategory : cmsArticleCategoryList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(cmsArticleCategory.getId());
            ztree.setpId(cmsArticleCategory.getParentId());
            ztree.setName(cmsArticleCategory.getTitle());
            ztree.setTitle(cmsArticleCategory.getTitle());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
